#include "cfgfile.h"

CFGFile::CFGFile()
{
    load();
}

void CFGFile::load()
{
    qDebug()<<"CFGFile::load()";
    QFile file("config.cfg");
    if(file.open(QFile::ReadOnly))
    {
        while (!file.atEnd())
        {
            QString line(file.readLine());
            line = line.trimmed();
            qDebug()<<"line:"<<line;
            if(line.at(0)!='#')
            {
                QStringList	attrValue( line.split("="));
                if(attrValue[0].trimmed()=="server_ip")
                {
                    this->setServerIP(attrValue[1].trimmed());
                    qDebug()<<"this->getServerIP:"<<this->getServerIP();
                }
                else if(attrValue[0].trimmed()=="server_port")
                {
                    this->setServerPort(attrValue[1].trimmed().toInt());
                    qDebug()<<"this->getServerPort:"<<this->getServerPort();
                }
                else if(attrValue[0].trimmed()=="server_path")
                {
                    this->setServerPath(attrValue[1].trimmed());
                    qDebug()<<"this->getServerPath:"<<this->getServerPath();
                }
                else if(attrValue[0].trimmed()=="server_monitor_path")
                {
                    this->setServerMonitorPath(attrValue[1].trimmed());
                    qDebug()<<"this->getServerMoitorPath:"<<this->getServerMonitorPath();
                }
                else if(attrValue[0].trimmed()=="server_monitor_ip")
                {
                    this->setServerMonitorIP(attrValue[1].trimmed());
                    qDebug()<<"this->getServerMonitorIP:"<<this->getServerMonitorIP();
                }
                else if(attrValue[0].trimmed()=="server_monitor_port")
                {
                    this->setServerMonitorPort(attrValue[1].trimmed().toInt());
                    qDebug()<<"this->getServerMonitorPort:"<<this->getServerMonitorPort();
                }
                else if(attrValue[0].trimmed()=="server_monitor_pass")
                {
                    this->setMonitorPass(attrValue[1].trimmed());
                    qDebug()<<"this->getMonitorPass:"<<this->getMonitorPass();
                }
                else if(attrValue[0].trimmed()=="server_monitor_user")
                {
                    this->setMonitorUser(attrValue[1].trimmed());
                    qDebug()<<"this->getMonitorUser:"<<this->getMonitorUser();
                }
                else if(attrValue[0].trimmed()=="id_device")
                {
                    this->setIdDevice(attrValue[1].trimmed());
                    qDebug()<<"this->getIdDevice:"<<this->getIdDevice();
                }
            }
        }
    }
}

void CFGFile::writeValue(QString name,QString value)
{
    qDebug()<<"CFGFile::load()";
    QFile file("config.cfg");
    QFile aux("tmp.cfg");
    if(file.open(QFile::ReadOnly|QFile::Text)&&aux.open(QFile::WriteOnly|QFile::Text))
    {
        bool found = false;
        while (!file.atEnd())
        {
            QString line(file.readLine());
            line = line.trimmed();
            qDebug()<<"line:"<<line;
            if(line.at(0)!='#')
            {
                QStringList	attrValue( line.split("="));
                if(attrValue[0].trimmed()==name)
                {
                    found = true;
                    line= name+"="+value;
                }
            }
            line +="\n";
            aux.write(line.toAscii());
        }
        if(!found)
        {
            QString line = name+"="+value;
            line +="\n";
            aux.write(line.toAscii());
        }
    }
    aux.close();
    file.close();
    file.remove();
    aux.rename(file.fileName());
}

void CFGFile::setServerIP(QString value)
{
    _serverIP = value;
}
void CFGFile::setServerPath(QString value)
{
    _serverPath = value;
}
void CFGFile::setServerPort(int value)
{
    _serverPort = value;
}

void CFGFile::setServerMonitorIP(QString value)
{
    _serverMonitorIP = value;
}

void CFGFile::setServerMonitorPort(int value)
{
    _serverMonitorPort = value;
}

void CFGFile::setServerMonitorPath(QString value)
{
    _serverMonitorPath = value;
}

void CFGFile::setMonitorUser(QString value)
{
    _monitorUser = value;
}

void CFGFile::setMonitorPass(QString value)
{
    _monitorPass = value;
}
void CFGFile::setIdDevice(QString value)
{
   _idDevice =  value;
}


QString CFGFile::getServerIP()
{
    return _serverIP;
}

int CFGFile::getServerPort()
{
    return _serverPort;
}

QString CFGFile::getServerPath()
{
    return _serverPath;
}

QString CFGFile::getServerMonitorIP()
{
    return _serverMonitorIP;
}

int CFGFile::getServerMonitorPort()
{
    return _serverMonitorPort;
}

QString CFGFile::getServerMonitorPath()
{
    return _serverMonitorPath;
}

QString CFGFile::getMonitorUser()
{
    return _monitorUser;
}

QString CFGFile::getMonitorPass()
{
    return _monitorPass;
}

QString CFGFile::getIdDevice()
{
    return _idDevice;
}


